package cn.mesmile.kafka.canal;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.alibaba.otter.canal.protocol.Message;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;

import java.net.InetSocketAddress;
import java.util.List;

/**
 * @author zb
 * @date 2022/1/9 14:42
 * @Description
 */
public class CanalTest {

    public static void main(String[] args) throws InterruptedException, InvalidProtocolBufferException {
        CanalConnector canalConnector = CanalConnectors.newSingleConnector(new InetSocketAddress("", 11111)
                , "example", "", "");

        while (true) {
            canalConnector.connect();
            // * 为匹配符号
            canalConnector.subscribe("数据库名称.*");
            // 获取数据
            Message message = canalConnector.get(100);

            List<CanalEntry.Entry> entries = message.getEntries();
            // 判断集合是否为空
            if (CollUtil.isEmpty(entries)) {
                System.out.println(">>>>> 没有数据则休息一下");
                Thread.sleep(1000);
            } else {
                for (CanalEntry.Entry entry : entries) {
                    // 获取表名
                    String tableName = entry.getHeader().getTableName();
                    // 获取类型
                    CanalEntry.EntryType entryType = entry.getEntryType();
                    // 获取序列化后的数据
                    ByteString storeValue = entry.getStoreValue();
                    // 判断当前entryType类型是否为 rowData
                    if (CanalEntry.EntryType.ROWDATA.equals(storeValue)) {
                        // 反序列化数据
                        CanalEntry.RowChange rowChange = CanalEntry.RowChange.parseFrom(storeValue);
                        // 获取当前事件的操作类型
                        CanalEntry.EventType eventType = rowChange.getEventType();
                        // 获取数据
                        List<CanalEntry.RowData> rowDataList = rowChange.getRowDatasList();
                        for (CanalEntry.RowData rowData : rowDataList) {
                            JSONObject beforeData = new JSONObject();
                            List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
                            for (CanalEntry.Column column : beforeColumnsList) {
                                beforeData.put(column.getName(), column.getValue());

                            }
                            JSONObject afterData = new JSONObject();
                            List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
                            for (CanalEntry.Column column : afterColumnsList) {
                                afterData.put(column.getName(), column.getValue());
                            }
                            // 数据打印
                            System.out.println("table:"+ tableName +" ,eventType:" + entryType);
                            // 数据
                            System.out.println("beforeData:"+beforeData +", afterData:"+ afterData);
                        }
                    }else {
                        System.out.println("当前操作类型为：" + entryType);
                    }



                }
            }


        }

    }
}
